package com.demo.user.service.handler.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.user.entity.DeptInfo;
import com.demo.user.entity.DeptInfoExtend;
import com.demo.user.mapper.DeptInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : YU.TAN
 * @date : 2021/8/27 11:25
 */
@Service
public class DeptInfoExtendTransfer extends BaseTransferProcess<DeptInfoExtend>{

    @Autowired
    private DeptInfoMapper deptInfoMapper;

    protected DeptInfoExtendTransfer(BaseMapper<DeptInfoExtend> baseMapper) {
        super(baseMapper);
    }

    @Override
    protected LambdaQueryWrapper<DeptInfoExtend> getQuery(Long userId) {
        LambdaQueryWrapper<DeptInfoExtend> wrapper = new LambdaQueryWrapper<>();
        List<DeptInfo> deptInfos = deptInfoMapper.selectList(new LambdaQueryWrapper<DeptInfo>().eq(DeptInfo::getCreateId, userId));
        if (!CollectionUtils.isEmpty(deptInfos)){
            wrapper.in(DeptInfoExtend::getDeptId,deptInfos.stream().map(DeptInfo::getId).collect(Collectors.toList()));
        }
        return wrapper;
    }

    @Override
    protected DeptInfoExtend getUpdate(Long newUserId) {
        DeptInfoExtend deptInfoExtend=new DeptInfoExtend();
        deptInfoExtend.setCreateId(newUserId);
        deptInfoExtend.setUpdateTime(LocalDateTime.now());
        return deptInfoExtend;
    }
}
